package com.example.wsappproject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Px;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.example.wsappproject.entity.KnowledgeTreeEntity;
import com.example.wsappproject.mainpagefragment.subpagefragment.adapter.SubPageFragmentStateAdapter;
import com.example.wsappproject.mainpagefragment.subpagefragment.adapter.SubPageIndicatorAdapter;
import com.example.wsappproject.mainpagefragment.subpagefragment.adapter.SubPageListFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SubPageActivity  extends AppCompatActivity {
    public static final String SUB_PAGE_TITLE = "sub_page_title";
    public static final String CHILDREN_BEAN = "children_bean";

    //标题显示控件
    private TextView tvTitle;
    //头部指示器列表控件
    private RecyclerView rvIndicator;
    //头部指示器适配器
    private SubPageIndicatorAdapter mIndicatorAdapter;
    private List<KnowledgeTreeEntity.DataBean.ChildrenBean> mChildrenBeanList;

    private List<SubPageListFragment> mSubPageFragmentList = new ArrayList<>();

    //Fragment切换使用的ViewPager
    private ViewPager vpContainer;
    //ViewPager适配器
    private SubPageFragmentStateAdapter mSubPageStateAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_page);
        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        tvTitle = findViewById(R.id.tv_title_activity_sub_page);
        rvIndicator = findViewById(R.id.rv_indicator_fragment_sub_page_list);
        vpContainer = findViewById(R.id.vp_container_activity_sub_page);
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            String title = intent.getStringExtra(SUB_PAGE_TITLE);
            mChildrenBeanList = (List<KnowledgeTreeEntity.DataBean.ChildrenBean>) intent.getSerializableExtra(CHILDREN_BEAN);

            tvTitle.setText(title);
        }

        rvIndicator.setLayoutManager(
                new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        mIndicatorAdapter = new SubPageIndicatorAdapter(rvIndicator, mChildrenBeanList);
        rvIndicator.setAdapter(mIndicatorAdapter);

        int size = mChildrenBeanList.size();
        /*
        此处出个题：
        subPageListFragment1中的rvFragmentSubPage对象,可以直接使用吗？
        或者，subPageListFragment1中的scrollToTop方法，可以直接调用吗？
        可以直接调用          1
        不可以直接调用        0
        答案：
        不可以直接调用
        答案解析：
        直接调用scrollToTop方法会导致空指针异常。
        Fragment中的onCreateView方法未执行
        为什么onCreateView方法未执行？
        Fragment没有和Activity进行关联。Fragment没有大哥Activity罩着。
         */
        SubPageListFragment subPageListFragment1 = new SubPageListFragment();
//        subPageListFragment1.scrollToTop();
        for (int i = 0; i < size; i++) {
            KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean = mChildrenBeanList.get(i);
            /*
            newInstance方法是静态方法，所以调用此方法不要用对象，
            就用类名来调用
             */
            SubPageListFragment subPageListFragment = SubPageListFragment.newInstance(childrenBean);
            /*
             * SubPageListFragment是类    可以理解为模板
             * subPageListFragment是对象  可以理解为通过模板创建的实物
             * 通过SubPageListFragment这个类创建的对象都是不一样的
             *
             * 想操作SubPageListFragment对象里面的变量，就需要先获取该对象
             * 想操作哪个对象里面的变量，就先获取到这个对象
             */
            mSubPageFragmentList.add(
                    SubPageListFragment.newInstance(mChildrenBeanList.get(i)));
        }

        mSubPageStateAdapter = new SubPageFragmentStateAdapter(
                getSupportFragmentManager(), mSubPageFragmentList);
        vpContainer.setAdapter(mSubPageStateAdapter);
    }

    private void initEvent() {
        mIndicatorAdapter.setOnIndicatorItemClickListener(
                new SubPageIndicatorAdapter.onIndicatorItemClickListener() {
                    @Override
                    public void onIndicatorItemClick(
                            int position,
                            KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean) {
                        vpContainer.setCurrentItem(position);
                    }
                });
        vpContainer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, @Px int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                mIndicatorAdapter.setCurrentItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }


    /**
     *
     * @param context  上下文菜单
     * @param title    SubPageActivity需要用到的第一个参数  标题名称
     * @param childrenBeanList SubPageActivity需要用到的第二个参数  tab指示器列表
     */
    public static void startSubPageActivity(
            Context context,
            String title,
            List<KnowledgeTreeEntity.DataBean.ChildrenBean> childrenBeanList) {
        Intent intent = new Intent(context, SubPageActivity.class);
        intent.putExtra(SUB_PAGE_TITLE, title);
        intent.putExtra(CHILDREN_BEAN, (Serializable) childrenBeanList);
        context.startActivity(intent);
    }
}
